package example.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mvel.MVEL;

public class MapExample {
	
	private static Log log = LogFactory.getLog(PropertyExample.class);
	
	public static void main(String[]args) {
		String result;
		Map vars = new HashMap();
		
		Map<String, String> map = new LinkedHashMap<String, String>();
		map.put("key1", "value1");
		map.put("key2", "value2");
		map.put("key3", "value3");
		
		vars.put("map", map);
		
		result = "\n"
			+ "The map is: \n" 
			+ "map[key1]=" + (String)MVEL.eval("map['key1']", vars) + "\n"
			+ "map.get('key2')=" + (String)MVEL.eval("map.get('key2')", vars) + "\n"
			+ "map.key3=" + (String)MVEL.eval("map.key3", vars);
		log.info(result);
		
		// =====================================================================
		User user1 = new User();
		user1.setName("kongxx");
		user1.setPassword("12345678");
		
		User user2 = new User();
		user2.setName("mandy");
		user2.setPassword("87654321");
		
		Map<String, User> users = new LinkedHashMap<String, User>();
		users.put(user1.getName(), user1);
		users.put(user2.getName(), user2);
		vars = new HashMap();
		vars.put("users", users);
		
		result = "\n"
			+ "The Map<name, User> is: \n" 
			+ "users['kongxx'].password=" + (String)MVEL.eval("users['kongxx'].password", vars) + "\n"
			+ "users['mandy'].password=" + (String)MVEL.eval("users['mandy'].password", vars);;
		log.info(result);
	}
}
